package com.zj.common.expand

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Point
import android.graphics.drawable.Drawable
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.StringRes
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.zj.common.utils.DebugUtils
import java.io.File
import java.time.Duration


// 状态栏高度
fun Context.statusBarHeight(): Int {
    val identifier = resources.getIdentifier("status_bar_height", "dimen", "android")
    return resources.getDimensionPixelSize(identifier)
}

// 屏幕宽度
fun Context.screenWidth(): Int {
    return resources.displayMetrics.widthPixels
}

fun Context.screenHeight(): Int {
    return resources.displayMetrics.heightPixels
}


// 根据url下载图片
fun Context.downloadImage(url: String): File? {
    return try {
        Glide.with(this)
            .asFile()
            .load(url)
            .submit()
            .get()
    } catch (e: Exception) {
        DebugUtils.printfError(e.message.toString())
        null
    }
}

/**
 * 根据url获取bitmap图片
 */
fun Context.getBitmap(url: String, callBack: (bitmap: Bitmap, width: Int, height: Int) -> Unit) {
    Glide.with(this)
        .asBitmap()
        .load(url)
        .into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                callBack.invoke(resource, resource.width, resource.height)
            }
            override fun onLoadCleared(placeholder: Drawable?) {
            }
        })
}

fun Context.toast(msg: String, duration: Int = Toast.LENGTH_LONG) {
    if (msg.isNotEmpty())
        main { Toast.makeText(this@toast, msg, duration).show() }
}

fun Context.toast(@StringRes msgRes: Int, duration: Int = Toast.LENGTH_SHORT) {
    main { Toast.makeText(this@toast, msgRes, duration).show() }
}
